package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.domain.SysOperLog;
import com.huike.clues.domain.dto.GetLogListDTO;
import com.huike.clues.domain.dto.SysOperLogDTO;
import com.huike.clues.domain.result.PageLogVO;
import com.huike.clues.domain.vo.SyslogPageVO;
import com.huike.clues.mapper.SysLogMapper;
import com.huike.clues.service.ISysLogService;
import com.huike.common.constant.ErrorMessageConstants;
import com.huike.common.exception.BaseException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description SysLogServiceImpl
 * @Author zhuzi
 * @Date 2023/10/17
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysOperLog> implements ISysLogService {

    private final SysLogMapper mapper;

    /**
     * 获取日志列表
     */
    @Override
    public PageLogVO getLogList(GetLogListDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<SyslogPageVO> page = mapper.getLogList(dto);
        return PageLogVO.builder()
                .code(200)
                .msg("查询成功")
                .rows(page.getResult())
                .total(page.getTotal())
                .build();
    }

    /**
     * 批量删除
     *
     * @param operIds 待删除的日志id集合
     */
    @Override
    public void delete(List<Long> operIds) {
        if (!removeByIds(operIds)) {
            throw new BaseException(ErrorMessageConstants.FAILED_TO_DELETE_LOG);
        }
    }

    /**
     * 清空日志
     */
    @Override
    public void clean() {
        mapper.clean();
    }

    /**
     * 获取导出日志列表
     */
    @Override
    public List<SysOperLogDTO> export(GetLogListDTO dto) {
        return mapper.getExportList(dto);
    }

}
